package cn.ultrarisk.scadmin.manager.web.controller.system;

import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrCollectionDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrCollection;
import cn.ultrarisk.scadmin.manager.biz.user.dao.PrivilegeDAO;
import cn.ultrarisk.scadmin.manager.biz.user.dao.RoleDAO;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Admin;
import cn.ultrarisk.scadmin.manager.biz.user.entity.Privilege;
import cn.ultrarisk.scadmin.manager.biz.user.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PrivilegeController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/5/3
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/system/privilege", produces = "application/json")
public class PrivilegeController {
    @Autowired
    private PrivilegeDAO privilegeDAO;
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private SolrCollectionDAO solrCollectionDAO;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Map<String, Object> list() {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Privilege> privileges = new ArrayList<Privilege>();
        Map<Integer, List<Integer>> collectionPrivilegeMap = new HashMap<Integer, List<Integer>>();
        Map<Integer, String> collectionMap = new HashMap<Integer, String>();

        Admin admin = SessionUtil.getAdmin();
        Map<Integer, Boolean> roles = roleDAO.findRoleByAdmin(admin.getId());
        Map<Integer, Map<Integer, Boolean>> collectionRoles = roleDAO.findCollectionRoleByAdmin(admin.getId());

        List<Integer> roleIds = new ArrayList<Integer>();
        roleIds.addAll(roles.keySet());
        roleIds.addAll(collectionRoles.keySet());
        if (roleIds.size() > 0) {
            privileges = privilegeDAO.assembly(privilegeDAO.findByRoleIds(roleIds));
        }

        if (collectionRoles.size() > 0) {
            Map<Integer, Integer> collectionPrivilegeRoleMap = privilegeDAO.getCollectionPrivilegeRoleMap(collectionRoles.keySet());

            for (Integer roleId : collectionRoles.keySet()) {
                for (Integer collectionId : collectionRoles.get(roleId).keySet()) {
                    List<Integer> collectionPrivilegeIds = collectionPrivilegeMap.get(collectionId);
                    if (collectionPrivilegeIds == null) {
                        collectionPrivilegeIds = new ArrayList<Integer>();
                    }

                    for (Integer collectionPrivilegeId : collectionPrivilegeRoleMap.keySet()) {
                        if (roleId.equals(collectionPrivilegeRoleMap.get(collectionPrivilegeId)) && !collectionPrivilegeIds.contains(collectionPrivilegeId)) {
                            collectionPrivilegeIds.add(collectionPrivilegeId);
                        }
                    }
                    collectionPrivilegeMap.put(collectionId, collectionPrivilegeIds);
                }
            }

            if (collectionPrivilegeMap.size() > 0) {
                List<SolrCollection> collections = solrCollectionDAO.findByIds(collectionPrivilegeMap.keySet(), SolrCollection.ID, SolrCollection.NAME);
                for (SolrCollection collection : collections) {
                    collectionMap.put(collection.getId(), collection.getName());
                }
            }
        }

        Map<String, Integer> collectionSortedMap = new HashMap<String, Integer>();
        for (Integer collectionId : collectionMap.keySet()) {
            collectionSortedMap.put(collectionMap.get(collectionId), collectionId);
        }
        result.put("privileges", privileges);
        result.put("collectionPrivilegeMap", collectionPrivilegeMap);
        result.put("collectionMap", collectionMap);
        result.put("collectionSortedMap", collectionSortedMap);

        return result;
    }
}
